package com.pxxy.lawconsult.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import com.pxxy.lawconsult.bean.ClientUser;
import com.pxxy.lawconsult.bean.LawerUser;
import com.pxxy.lawconsult.bean.LoginData;
import com.pxxy.lawconsult.bean.User;
import com.pxxy.lawconsult.constant.DBConstant;
import com.pxxy.lawconsult.utils.DBUtils;
import com.pxxy.lawconsult.utils.SetBeanUtils;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	private Gson gson;
	private LoginData data;

	public LoginServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		String tel = request.getParameter(User.TEL);
		String password = request.getParameter(User.PASSWORD);
		gson = new Gson();
		data = new LoginData();

		PrintWriter out = response.getWriter();

		if (tel == null || tel.equals("")) {
			data.setLoginState(LoginData.LOGINSTATE_FAILURE);
			out.write(gson.toJson(data));
			return;
		} else if (password == null || password.equals("")) {
			data.setLoginState(LoginData.LOGINSTATE_FAILURE);
			out.write(gson.toJson(data));
			return;
		}

		// 验证用户和密码
		try {
			ResultSet result = DBUtils.query(DBConstant.TB_USER_TABLE, DBConstant.USER_TABLE_TEL, tel);
			if (result.next()) {
				if (password.equals(result.getString(DBConstant.USER_TABLE_PASSWORD))) {
					if (result.getInt(DBConstant.USER_TABLE_TYPE) == 0) {
						ClientUser user = SetBeanUtils.loginClientUser(result);
						// 设置请求成功
						data.setLoginState(LoginData.LOGINSTATE_SUCCESS);
						// 设置用户数据
						data.setClientUser(user);
						data.setType(0);
					} else if (result.getInt(DBConstant.USER_TABLE_TYPE) == 1) {
						LawerUser user = SetBeanUtils.loginLawerUser(result);
						// 设置请求成功
						data.setLoginState(LoginData.LOGINSTATE_SUCCESS);
						// 设置用户数据
						data.setLawerUser(user);
						data.setType(1);
					} else if (result.getInt(DBConstant.USER_TABLE_TYPE) == 2) {
						
					}
					System.out.println("1");
				} else {
					// 密码错误
					data.setLoginState(LoginData.LOGINSTATE_FAILURE);
					System.out.println("2");
				}
			} else {
				// 用户不存在
				data.setLoginState(LoginData.LOGINSTATE_FAILURE);
				System.out.println("3");
			}
		} catch (SQLException e) {
			data.setLoginState(LoginData.LOGINSTATE_FAILURE_SERVER_ERROR);
			e.printStackTrace();
		} finally {
			// 转化为json对象
			String userJson = gson.toJson(data);
			// 返回json数据
			System.out.println(userJson);
			out.println(userJson);
			// 关闭数据库连接
			DBUtils.close();
			out.close();
		}
	}

}
